package com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.controller;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.domain.Frontdesk_sreen_delete_videoStream;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.mapper.Frontdesk_cameraMapper;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.service.Frontdesk_sreenVideoSream_deleteService;
import org.springframework.http.HttpMethod;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.domain.Frontdesk_screen_VideoSteam;
import com.ruoyi.jeeplus.modules.screen.frontdesk.screen_VideoStream.service.Frontdesk_screen_VideoStreamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 刘奥旸
 * @version 1.0
 */
@Controller
public class Frontdesk_VideoStream_utils {
    @Autowired
    Frontdesk_screen_VideoStreamService frontdeskScreenVideoStreamService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private Frontdesk_sreenVideoSream_deleteService frontdeskSreenVideoSreamDeleteService;
    @Autowired
    Frontdesk_cameraMapper frontdeskCameraMapper;
//        private volatile boolean isFirstTaskCompleted = false; // 标志第一个任务是否完成
//
//    // 第一个定时任务：每天固定时间执行
//    @Scheduled(fixedRate = 5000)
//    public void firstTask() {
//        System.out.println("First task started.");
//
//
//        System.out.println("First task completed.");
//        isFirstTaskCompleted = true; // 设置标志
//    }
//
//    // 第二个定时任务：第一个任务完成后一秒执行
//    @Scheduled(fixedDelay = 1000, initialDelay = 0) // 每隔 1 秒检查一次
//    public void secondTask() {
//        if (isFirstTaskCompleted) {
//            System.out.println("Second task started.");
//            // 执行任务逻辑
//            isFirstTaskCompleted = false; // 重置标志
//            System.out.println("Second task completed.");
//        }
//    }


// @GetMapping(value = "/lay/deletevideo")
//@GetMapping(value = "/lay/deletev")
    private String deletevideo(String station_id){
//        String station_id = new String("1");
        Frontdesk_sreen_delete_videoStream videoSteam = frontdeskSreenVideoSreamDeleteService.getVideoSteam(station_id);
        System.out.println(videoSteam);
        // 拼接 URL
        String url = String.format("http://third.platform.xstrive.com/product/delAddress?cId=%d&channel=%s&pId=%s&token=%s&t=%d&vId=%s",
                videoSteam.getCId(),
                videoSteam.getChannel(),
                videoSteam.getPId(),
                videoSteam.getToken(),
                videoSteam.getT(),
                videoSteam.getVId()
        );
//        System.out.println(url);
        // 创建一个空的 HttpEntity，不传递任何数据
        HttpEntity<String> entity = new HttpEntity<>(null);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        // 打印响应结果
        frontdeskCameraMapper.updateTokenAndCreatedAt(station_id,"LAYH_null");
        frontdeskCameraMapper.updateFinalUrlByStationId(station_id,"LAYH_null");
        System.out.println("Response: " + response.getBody());

        return response.getBody();

    }

//@GetMapping("/lay/getvideo")
//@GetMapping("/lay/getv")
    public String getvideo(String station_id) {
//        String station_id = new String("1");
        String video_id = frontdeskCameraMapper.selectCameraIdByStationId(station_id);
//        String video_id = new String("475537b187d31432d017ae3c43ea7698");//这个需要动态
        Frontdesk_screen_VideoSteam videoSteam = frontdeskScreenVideoStreamService.getVideoSteam(video_id);
// 拼接 URL
        String url = String.format("http://third.platform.xstrive.com/product/genAddress?cId=%d&channel=%s&des=%s&expireldSec=%d&genType=%s&pId=%s&token=%s&t=%d",
                videoSteam.getCId(),
                videoSteam.getChannel(),
                videoSteam.getDes(),
                videoSteam.getExpiredSec(),
                videoSteam.getGenType(),
                videoSteam.getPId(),
                videoSteam.getToken(),  // 如果 token 存储在 `videoSteam` 中
                videoSteam.getT()
        );
// 创建空的 HttpEntity，因为请求体为空
        System.out.println(url);
        // 创建一个空的 HttpEntity，不传递任何数据
        HttpEntity<String> entity = new HttpEntity<>(null);

        // 发送 POST 请求
//        ResponseEntity<String> response = restTemplate.exchange("http://jsonplaceholder.typicode.com/posts", HttpMethod.POST, entity, String.class);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        // 打印响应结果
//        System.out.println("Response: " + response.getBody());
//        System.out.println("主要"+extractToken(response.getBody()));
        String final_url = "https://flv.cloud.xstrive.com/" + video_id +"/hlsram/live0/" + extractToken(response.getBody()) + ".flv";
//        System.out.println(extractToken(response.getBody()));
        frontdeskCameraMapper.updateTokenAndCreatedAt(station_id,extractToken(response.getBody()));
        System.out.println(final_url);
        frontdeskCameraMapper.updateFinalUrlByStationId(station_id,final_url);
        return final_url;  // 返回请求的响应内容
    }





    // 提取token
    public static String extractToken(String path) {
        String token = null;
        Pattern pattern = Pattern.compile("token=([a-f0-9]+)");
        Matcher matcher = pattern.matcher(path);
        if (matcher.find()) {
            token = matcher.group(1); // 提取token
        }
        return token;
    }


}
